package bean;

import javax.servlet.ServletException;

import org.apache.commons.codec.digest.DigestUtils;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import dao.CustomerDAO;
import dao.EmployeeDAO;
import dao.FundDAO;
import dao.FundPriceHistoryDAO;
import dao.FundTransactionDAO;
import dao.LoginDAO;
import dao.PasswordDAO;
import dao.PositionDAO;
import dao.UserDAO;


public class Model {
	private SessionFactory sessionFactory;
	
	private LoginDAO loginDAO = null;
	private PasswordDAO passwordDAO = null;
	private FundDAO fundDAO = null;

	private EmployeeDAO employeeDAO = null;
	private CustomerDAO customerDAO = null; 
	private UserDAO userDAO = null;
	
	//Added by Sahil
	private FundTransactionDAO fundTransactionDAO = null;
	private FundPriceHistoryDAO fundPriceHistoryDAO = null;
	private PositionDAO positionDAO = null;
	
//	private TransactionDAO transactionDAO = null;
	
	public Model(DBConfigure configure) throws ServletException {		
		 try {
//	            Create the SessionFactory from hibernate.cfg.xml
//			 	Class.forName("com.mysql.jdbc.Driver");
				Configuration cfg = new Configuration();
		        cfg.setProperty("hibernate.connection.url", configure.getUrl());
		        cfg.setProperty("hibernate.connection.username", configure.getUserName());
		        cfg.setProperty("hibernate.connection.password", configure.getPassword());
		        cfg.configure();
		        ServiceRegistry serviceRegistery = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
		        sessionFactory = cfg.configure().buildSessionFactory(serviceRegistery);
	            
		        loginDAO = new LoginDAO(sessionFactory);
		        passwordDAO = new PasswordDAO(sessionFactory);
		        fundDAO = new FundDAO(sessionFactory);
		        customerDAO = new CustomerDAO(sessionFactory);

		        employeeDAO = new EmployeeDAO(sessionFactory);
		        userDAO = new UserDAO(sessionFactory);
		        //fundTransactionDAO = new FundTransactionDAO(sessionFactory);
		        fundPriceHistoryDAO = new FundPriceHistoryDAO(sessionFactory);
		        positionDAO = new PositionDAO(sessionFactory);
		        fundTransactionDAO = new FundTransactionDAO(sessionFactory, customerDAO, positionDAO);
		        
		        Employee e = new Employee();

		        e.setFirstname("Admin");
		        e.setLastname("Admin");
		        e.setUsername("admin");
		        e.setPassword("admin");
		        String adminName = "admin";
		        e.setFirstname(adminName);
		        e.setLastname(adminName);
		        e.setUsername(adminName);
		        e.setPassword(DigestUtils.md5Hex(adminName));
		        
		        employeeDAO.initialEmployee(e);
		        User admin = userDAO.lookUpByUsername(adminName);
		        if(admin == null)
		        {
		        	e = employeeDAO.lookUpByUsername(adminName);
		        	admin = new User();
		        	admin.setCustomer(false);
		        	admin.setUserId(e.getId());
		        	admin.setUsername(e.getUsername());
		        	userDAO.create(admin);
		        }
	        }
	        catch (Throwable ex) {
	            // Make sure you log the exception, as it might be swallowed
	            System.err.println("Initial SessionFactory creation failed." + ex);
	            throw new ExceptionInInitializerError(ex);
	        }
	}
	
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
    public LoginDAO getLoginDAO()
    {
    	return loginDAO;
    }

	public PasswordDAO getPasswordDAO() {
		return passwordDAO;
	}
	
	public FundDAO getFundDAO() {
		return this.fundDAO;
	}

	public FundPriceHistoryDAO getFundPriceHistoryDAO() {
		return fundPriceHistoryDAO;
	}

	public EmployeeDAO getEmployeeDAO() {
		return employeeDAO;
	}
	
	public CustomerDAO getCustomerDAO() {
		return this.customerDAO;
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public FundTransactionDAO getFundTransactionDAO(){
		return this.fundTransactionDAO;
	}

	public PositionDAO getPositionDAO(){
		return this.positionDAO;
	}

}
